package com.yehongyu.mytools.ui.awt;

//: Menu1.java
//Menus work only with Frames.
//Shows submenus, checkbox menu items
//and swapping menus.
import java.awt.Button;
import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Menu1 extends Frame implements ActionListener {
	/**序列ID*/
	private static final long serialVersionUID = 1L;

	String[] flavors = { "Chocolate", "Strawberry", "Vanilla Fudge Swirl",
			"Mint Chip", "Mocha Almond Fudge", "Rum Raisin", "Praline Cream",
			"Mud Pie" };

	TextField t = new TextField("No flavor", 30);

	MenuBar mb1 = new MenuBar();

	Menu f = new Menu("File");
	Menu m = new Menu("Flavors");
	Menu s = new Menu("Safety");

	// Alternative approach:
	CheckboxMenuItem[] safety = { new CheckboxMenuItem("Guard"),
			new CheckboxMenuItem("Hide") };
	MenuItem[] file = { new MenuItem("Open"), new MenuItem("Exit") };

	// A second menu bar to swap to:
	MenuBar mb2 = new MenuBar();
	Menu fooBar = new Menu("fooBar");
	MenuItem[] other = { new MenuItem("Foo"), new MenuItem("Bar"),
			new MenuItem("Baz"), };

	Button b = new Button("Swap Menus");

	public Menu1() {
		for (int i = 0; i < flavors.length; i++) {
			m.add(new MenuItem(flavors[i]));
			// Add separators at intervals:
			if ((i + 1) % 3 == 0)
				m.addSeparator();
		}
		for (int i = 0; i < safety.length; i++)
			s.add(safety[i]);
		f.add(s);
		for (int i = 0; i < file.length; i++)
			f.add(file[i]);
		mb1.add(f);
		mb1.add(m);
		setMenuBar(mb1);
		t.setEditable(false);
		add("Center", t);
		// Set up the system for swapping menus:
		add("North", b);
		b.addActionListener(this);
		for (int i = 0; i < other.length; i++)
			fooBar.add(other[i]);
		mb2.add(fooBar);
		f.addActionListener(this);
		m.addActionListener(this);
		s.addActionListener(this);
		fooBar.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(b)) {
			MenuBar m = getMenuBar();
			if (m == mb1)
				setMenuBar(mb2);
			else if (m == mb2)
				setMenuBar(mb1);
		} else if (e.getSource() instanceof MenuItem) {
			if (e.getSource().equals("Open")) {
				String s = t.getText();
				boolean chosen = false;
				for (int i = 0; i < flavors.length; i++)
					if (s.equals(flavors[i]))
						chosen = true;
				if (!chosen)
					t.setText("Choose a flavor first!");
				else
					t.setText("Opening " + s + ". Mmm, mm!");
			} else if (e.getSource().equals(file[1]))
				System.exit(0);
			// CheckboxMenuItems cannot use String
			// matching; you must match the target:
			else if (e.getSource().equals(safety[0]))
				t.setText("Guard the Ice Cream! " + "Guarding is "
						+ safety[0].getState());
			else if (e.getSource().equals(safety[1]))
				t.setText("Hide the Ice Cream! " + "Is it cold? "
						+ safety[1].getState());
			else
				t.setText(e.getSource().toString());
		} else {
			System.out.println("Object is [" + e.getSource()
					+ "],Unknown Action!");
		}

	}

	public static void main(String[] args) {
		Menu1 f = new Menu1();
		f.setSize(300, 200);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

}
